package com.lei.demo.service.imp;

import com.lei.demo.dao.BudgetDAO;
import com.lei.demo.entity.Budget;
import com.lei.demo.service.BudgetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Shinelon on 2017/6/15.
 */
@Service("BudgetService")
@Transactional(readOnly = true)
public class BudgetServiceImpl implements BudgetService {

    @Autowired
    BudgetDAO budgetDAO;



    @Override
    @Transactional(readOnly = false)
    public void delete(int buid) {
        List<Budget> budgets = findBudgets(buid);
        for (Budget budget : budgets) {
            budgetDAO.delete(budget.getBuid());
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void save(Budget budget) {
        budgetDAO.save(budget);
    }

    @Override
    public void update(Budget budget) {
        budgetDAO.update(budget);
    }

    @Override
    public Budget findById(int buid) {
        List<Budget> budgets = findBudgets(buid);
        if (!budgets.isEmpty()) {
            return budgets.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<Budget> findBudgets(int buid) {
        return budgetDAO.findBudget(buid);
    }

    @Override
    public List<Budget> findBudgets(String username) {
        return budgetDAO.findBudget(username);
    }

    @Override
    public List<Budget> getAllBudget() {
        return budgetDAO.getAllBudget();
    }

    @Transactional(readOnly = false)
    public void deleteBudget (Budget budget) {
        budgetDAO.delete(budget.getBuid());
    }
}
